package lxx.pandams.activity;

import java.util.List;
import lxx.pandams.R;
import lxx.pandams.bean.BlackNumberInfo;
import lxx.pandams.dao.CommSafeDao;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class CommSafeActivity extends Activity {
	
	protected static final int DATAISGETED = 102;
	private Context ctx;
	private Button bt_commsafe_add;
	private int mode = 1;
	private AlertDialog dialog;
	private List<BlackNumberInfo> list;
	private MyAdapter mAdapter;
	private ListView lv_commsafe_blacknumber;
	//use Handler to receive Message and update UI;
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
				case 102:
					if(mAdapter == null){
						mAdapter = new MyAdapter();
						lv_commsafe_blacknumber.setAdapter(mAdapter);
					}else{
						mAdapter.notifyDataSetChanged();
					}
					break;
			}
		};
	};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_commsafe);
		ctx = this;
		initUI();
		initData();
		initAction();
	}
	
	
	//initialize view in this layout
	private void initUI() {
		bt_commsafe_add = (Button) findViewById(R.id.bt_commsafe_add);
		lv_commsafe_blacknumber = (ListView) findViewById(R.id.lv_commsafe_blacknumber);
	}
	//get data from DataBase,send it to the main thread.
	private void initData() {
		new Thread(){			
			public void run() {
				list = CommSafeDao.getInstance(ctx).findAll();
				Message msg = Message.obtain();
				msg.what = DATAISGETED;
				handler.sendMessage(msg);
			};
		}.start();
	}
	
	private void initAction() {
		//set listener which is about show dialog for bt_commsafe_add 
		bt_commsafe_add.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog();
			}	
		});
	}
	
	//initialize dialog
	private void showDialog() {
		Builder builder = new AlertDialog.Builder(this);
		dialog = builder.create();
		View view = View.inflate(ctx, R.layout.dialog_add_blacknumber, null);
		dialog.setView(view);
		initViewUIandAction(view);
		dialog.show();
	}


	//initialize view in dialog and set listener for them
	private void initViewUIandAction(View view) {
		final EditText et_phone = (EditText) view.findViewById(R.id.et_phone);
		RadioGroup rg_group = (RadioGroup) view.findViewById(R.id.rg_group);
		Button bt_submit = (Button) view.findViewById(R.id.bt_submit);
		Button bt_cancel = (Button)view.findViewById(R.id.bt_cancel);
		
		//set mode according to RadioGroup's setting
		rg_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId){
					case R.id.rb_sms:
						//intercept message
						mode = 1;
						break;
					case R.id.rb_phone:
						//intercept phone
						mode = 2;
						break;
					case R.id.rb_all:
						//intercept all
						mode = 3;
						break;
				}
			}
		});
		//set listener for bt_submit
		bt_submit.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//get data from et_phone,rg_group.
				String phone = et_phone.getText().toString();
				//if not equals null.
				if(!TextUtils.isEmpty(phone)){
					CommSafeDao.getInstance(ctx).insert(phone, mode+"");
					BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
					blackNumberInfo.setPhone(phone);
					blackNumberInfo.setMode(mode+"");
					list.add(0,blackNumberInfo);
					if(mAdapter!=null){
						mAdapter.notifyDataSetChanged();
					}
					//hide dialog
					dialog.dismiss();
				}else{
					Toast.makeText(ctx, "请输入号码", 0).show();
				}
			}
		});
		//set listener for bt_cancel
		bt_cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//hide dialog
				dialog.dismiss();
			}
		});
	}
	//set Adapter for lv_commsafe_blacknumber
	class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;
			if(convertView == null){
				convertView = View.inflate(getApplicationContext(), R.layout.listview_blacknumber_item, null);
				//2,减少findViewById()次数
				//复用viewHolder步骤三
				holder = new ViewHolder();
				//复用viewHolder步骤四
				holder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
				holder.tv_mode = (TextView)convertView.findViewById(R.id.tv_mode);
				holder.iv_delete = (ImageView)convertView.findViewById(R.id.iv_delete);
				//复用viewHolder步骤五
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			holder.iv_delete.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//delete this data in database;
					CommSafeDao.getInstance(ctx).delete(list.get(position).phone);
					//remove this data in list
					list.remove(position);
					//notify adapter to update
					if(mAdapter!=null){
						mAdapter.notifyDataSetChanged();
					}
				}
			});
			
			holder.tv_phone.setText(list.get(position).phone);
			int mode = Integer.parseInt(list.get(position).mode);
			switch (mode) {
			case 1:
				holder.tv_mode.setText("拦截短信");
				break;
			case 2:
				holder.tv_mode.setText("拦截电话");
				break;
			case 3:
				holder.tv_mode.setText("拦截所有");
				break;
			}
			return convertView;
		}
	}
	//复用viewHolder步骤二
	static class ViewHolder{
		TextView tv_phone;
		TextView tv_mode;
		ImageView iv_delete;
	}
}
